<template>
    <div class="date-choose">
        <div class="choose">
            <div class="label">时段</div>
            <el-date-picker v-model="dateValue" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 400px" @change="dateChange" />
        </div>
        <div class="time-btn">
            <div class="item" :class="{ 'item-active': index === timeIndex }" v-for="(item, index) in time" :key="item" @click="itemClick(index)">{{ item }}</div>
            <w-button iconName="icon-shuaxin" style="width: 64px; height: 30px" @click="refresh">刷新</w-button>
        </div>
    </div>
</template>

<script setup>
import { ref } from "vue";
import wButton from "@/components/wButton.vue";
import dayjs from "dayjs";

const emits = defineEmits(["refresh", "dateChoose"]);
const startDate = dayjs().startOf("day").subtract(1, "day").format();
const endDate = dayjs().endOf("day").subtract(1, "day").format();
const dateValue = ref([startDate, endDate]);
const time = ["昨日", "本周", "本月", "今年"];
const timeIndex = ref(0);
const itemClick = (index) => {
    timeIndex.value = index;
    switch (index) {
        case 0:
            dateValue.value = [startDate, endDate];
            break;
        case 1:
            dateValue.value = [dayjs().startOf("week").add(1, "day").format(), dayjs().format()];
            break;
        case 2:
            dateValue.value = [dayjs().startOf("month").format(), dayjs().format()];
            break;
        case 3:
            dateValue.value = [dayjs().startOf("year").format(), dayjs().format()];
            break;
    }
    emits("dateChoose", dateValue.value);
};
itemClick(0);
const dateChange = (value) => {
    timeIndex.value = -1;
    emits("dateChoose", value);
};
const refresh = () => {
    emits("refresh", dateValue.value);
};
</script>

<style lang="less" scoped>
.date-choose {
    display: flex;
    align-items: center;
    .choose {
        display: flex;
        align-items: center;
        .label {
            margin-right: 16px;
            font-size: 14px;
            font-weight: 600;
            color: #282828;
        }
    }
    .time-btn {
        display: flex;
        align-items: center;
        justify-content: space-around;
        .item {
            margin-left: 15px;
            width: 64px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border-radius: 4px;
            border: 1px solid #dcdcdc;
            color: #999;
            font-size: 12px;
            cursor: pointer;
        }
        .item-active {
            background: @main-color;
            color: #fff;
            border: 1px solid @main-color;
        }
    }
}
</style>
